package com.excesys.exsecs.scada.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.scada.entity.ScadaDevice;
import com.excesys.exsecs.scada.entity.ScadaFolder;
import com.excesys.exsecs.scada.entity.ScadaProperty;
import com.excesys.exsecs.scada.mapper.ScadaDeviceMapper;
import com.excesys.exsecs.scada.service.ScadaFolderService;
import com.excesys.exsecs.scada.service.ScadaPropertyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件夹_
 *
 * @author pig
 * @date 2024-06-25 09:18:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/scadaFolder" )
@Tag(description = "scadaFolder" , name = "文件夹_管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ScadaFolderController {

    private final  ScadaFolderService scadaFolderService;
	private final ScadaPropertyService scadaPropertyService;
	private final ScadaDeviceMapper scadaDeviceMapper;

	/**
	 * 获取树
	 * @param scadaFolder
	 * @return
	 */
	@Operation(summary = "获取树" , description = "获取树" )
	@GetMapping("/tree" )
	public R getTree(@ParameterObject ScadaFolder scadaFolder){
		return R.ok(scadaFolderService.queryTree(scadaFolder),"查询树");
	}

	@Operation(summary = "获取树" , description = "获取树" )
	@GetMapping("/firstTree" )
	public R getFirstTree(@ParameterObject ScadaFolder scadaFolder){
		return R.ok(scadaFolderService.getFirstTree(scadaFolder),"最外层");
	}
	/**
     * 分页查询
     * @param page 分页对象
     * @param scadaFolder 文件夹_
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    //@PreAuthorize("@pms.hasPermission('excesysiot_scadaFolder_view')" )
    public R getScadaFolderPage(@ParameterObject Page page, @ParameterObject ScadaFolder scadaFolder) {
        LambdaQueryWrapper<ScadaFolder> wrapper = Wrappers.lambdaQuery();
        return R.ok(scadaFolderService.page(page, wrapper));
    }


    /**
     * 通过id查询文件夹_
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    //@PreAuthorize("@pms.hasPermission('excesysiot_scadaFolder_view')" )
    public R getById(@PathVariable("id" ) Long id) {
		ScadaFolder scadaFolder = scadaFolderService.getById(id);
		Long parentId = scadaFolder.getParentId();
		if(ObjectUtil.isNotNull(parentId) && !parentId.equals(-1L)){
			ScadaFolder parent = scadaFolderService.getById(parentId);
			scadaFolder.setParentName(parent.getFolderName());
		}
		return R.ok(scadaFolder);
    }

    /**
     * 新增文件夹_
     * @param scadaFolder 文件夹_
     * @return R
     */
    @Operation(summary = "新增文件夹_" , description = "新增文件夹_" )
    @SysLog("新增文件夹_" )
    @PostMapping
    //@PreAuthorize("@pms.hasPermission('excesysiot_scadaFolder_add')" )
    public R save(@RequestBody ScadaFolder scadaFolder) {
        return R.ok(scadaFolderService.save(scadaFolder),"新增文件夹");
    }

    /**
     * 修改文件夹_
     * @param scadaFolder 文件夹_
     * @return R
     */
    @Operation(summary = "修改文件夹_" , description = "修改文件夹_" )
    @SysLog("修改文件夹_" )
    @PutMapping
    //@PreAuthorize("@pms.hasPermission('excesysiot_scadaFolder_edit')" )
    public R updateById(@RequestBody ScadaFolder scadaFolder) {
        return R.ok(scadaFolderService.updateById(scadaFolder),"修改文件夹");
    }

    /**
     * 通过id删除文件夹_
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除文件夹_" , description = "通过id删除文件夹_" )
    @SysLog("通过id删除文件夹_" )
    @DeleteMapping
    //@PreAuthorize("@pms.hasPermission('excesysiot_scadaFolder_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(scadaFolderService.removeFoldersByIds(CollUtil.toList(ids)),"递归删除文件夹");
    }


    /**
     * 导出excel 表格
     * @param scadaFolder 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    //@PreAuthorize("@pms.hasPermission('excesysiot_scadaFolder_export')" )
    public List<ScadaFolder> export(ScadaFolder scadaFolder,Long[] ids) {
        return scadaFolderService.list(Wrappers.lambdaQuery(scadaFolder).in(ArrayUtil.isNotEmpty(ids), ScadaFolder::getId, ids));
    }

	/**
	 * 组态下拉列表
	 * @return
	 */
	@Operation(summary = "组态文件下拉列表" , description = "组态文件下拉列表" )
	@GetMapping("/getScadaInfo")
	public R getScadaInfo(){
//		List<ScadaFolder> scadaInfos = scadaFolderService.list(Wrappers.<ScadaFolder>lambdaQuery().ne(ScadaFolder::getScadaId, null));
//		List<Long> scadaIds = scadaInfos.stream().map(ScadaFolder::getScadaId).collect(Collectors.toList());
		QueryWrapper<ScadaDevice> wrapper = new QueryWrapper<>();
		wrapper.select("DISTINCT(scada_id) AS scadaId");

		List<Long> bindIds = scadaDeviceMapper.selectMaps(wrapper).stream()
				.map(it -> (Long)it.get("scadaId")).collect(Collectors.toList());

		List<ScadaProperty> list = scadaPropertyService.list(Wrappers.<ScadaProperty>lambdaQuery().in(ScadaProperty::getId,bindIds));
		//.notIn(CollectionUtils.isNotEmpty(scadaIds), ScadaProperty::getId, scadaIds)
		return R.ok(list,"组态文件下拉列表");
	}
}